/**
 * 
 */
$(initSelect());
function initSelect(){
	$.ajax({
		type : "post",
		url : "GetUserTeamListServlet",
		data : {user_id:user_id},
		dateType : "json",
		success : function(data) {
			var jsondata = eval('(' + data + ')');
			$.each(jsondata.teamArray,function (n,v){
				$("#choose-subgroup").append("<option value=\""+v.ID+"\">"+v.Name+"</option>");
			});
		},
		error : function() {
			// TODO do something
		}
	});
}

function addActor(){
	var newActorNode=$("<div></div>").addClass("invite-item");
	newActorNode.append("<img class=\"avatar\" src=\"../images/avatar.gif\">"+
						"<div class=\"invite-field\">"+
						"<input name=\"actot_name\" type=\"text\" class=\"invite-email no-border\""+
							"placeholder=\"请输入新成员的昵称\">"+
						"<div class=\"invite-role-field\"></div>"+
						"<input name=\"actor_desc\" type=\"text\" class=\"invite-comment no-border\""+
							"placeholder=\"添加成员备注（可选）\" data-validate=\"length:0,128\""+
						"data-validate-msg=\"成员备注最长128个字符\"></div>");
	var arrayNode=$("#add-actor-content").append(newActorNode);
	arrayNode.children(".invite-item").each(function (){
		if($(this).children(".del-invite").length==0){
			$(this).append("<a class=\"del-invite\" onclick=\"delActor($(this))\" href=\"javascript:;\">删除</a>");
		}
	});
}
function delActor(delNode){
	delNode.parent("div.invite-item").remove();
	var insNode=$("#add-actor-content").children("div.invite-item");
	if(insNode.length==1){
		insNode.children("a.del-invite").remove();
	}
}
$(function changeSelect(){
	$("#choose-subgroup").change(function() {
		var teamID=$(this).val();
		$.ajax({
			type : "post",
			url : "GetTeamActorServlet",
			data : {teamID:teamID},
			dateType : "json",
			success : function(data) {
				var jsondata = eval('(' + data + ')');
				var arrayNode=$("#add-actor-content");
				arrayNode.children().remove();
				if(jsondata.tUserArray.length==0){
					var newActorNode=$("<div></div>").addClass("invite-item");
					newActorNode.append("<img class=\"avatar\" src=\"../images/avatar.gif\">"+
										"<div class=\"invite-field\">"+
										"<input name=\"actot_name\" type=\"text\" class=\"invite-email no-border\""+
											"placeholder=\"请输入新成员的邮箱\">"+
										"<div class=\"invite-role-field\"></div>"+
										"<input name=\"actor_desc\" type=\"text\" class=\"invite-comment no-border\""+
											"placeholder=\"添加成员备注（可选）\" data-validate=\"length:0,128\""+
										"data-validate-msg=\"成员备注最长128个字符\"></div>");
					$("#add-actor-content").append(newActorNode);
				}
				$.each(jsondata.tUserArray,function (n,v){
					var newActorNode=$("<div></div>").addClass("invite-item");
					newActorNode.append("<img class=\"avatar\" src=\"../userIcons/"+v.userIcon+"\">"+
										"<div class=\"invite-field\">"+
										"<input name=\"actot_name\" type=\"text\" class=\"invite-email no-border\""+
											"placeholder=\"请输入新成员的昵称\" value=\""+v.userNickName+"\">"+
										"<div class=\"invite-role-field\"></div>"+
										"<input name=\"actor_desc\" type=\"text\" class=\"invite-comment no-border\""+
											"placeholder=\"添加成员备注（可选）\" data-validate=\"length:0,128\""+
										"data-validate-msg=\"成员备注最长128个字符\" value=\""+v.desc+"\"></div>");
					newActorNode.append("<a class=\"del-invite\" onclick=\"delActor($(this))\" href=\"javascript:;\">删除</a>");
					arrayNode.append(newActorNode);
				});
			},
			error : function() {
				// TODO do something
			}
		});
	});
});
function postProj(){
	var userName=new Array();
	var userDesc=new Array();
	var projTitle,projDesc;
	$(":input").each(function(n,b){
		var inputName=$(b).attr("name");
		if(inputName=='actot_name'){
			userName.push($(b).val());
		}
		if(inputName=='actor_desc'){
			userDesc.push($(b).val());
		}
	});
	projTitle=$("#project-name").val();
	projDesc=$("#project-desc").val();
	if(!judgeinput(userName,projTitle,projDesc)){
		return false;
	}
	$.ajax({
		type : "post",
		url : "PutCoopProjServlet",
		data : {userName:userName,userDesc:userDesc,projTitle:projTitle,projDesc:projDesc,user_id:user_id},
		dateType : "json",
		success : function(data) {
			var jsondata = eval('(' + data + ')');
			if(jsondata.isSuccess=="true"){
				if (confirm("创建 成功!")) {
					//跳转至协作学习平台首页
					location.href='../communication/cooperation.html';
				}
			}else{
				confirm("创建失败 ! 请重试...");
			}
		},
		error : function() {
			// TODO do something
		}
	});
}
function judgeinput(userName,projTitle,projDesc){
	if(!projTitle){
		confirm("请输入  项目名称");
		return false;
	}
	if(!projDesc){
		confirm("请输入  项目描述");
		return false;
	}
	if(userName.length==0){
		confirm("请添加  成员");
		return false;
	}
	return true;
}